package tp.pr5.gui;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;

import tp.pr5.PlaceInfo;

public class PlaceCell extends JButton
{
	private static final long serialVersionUID = 1L;
	
	private PlaceInfo placeInfo;
	private NavigationPanel navigationPanel;
	
	/**
	 * Default constructor
	 * 
	 */
	
	public PlaceCell()
	{
		super();
		
	}
	
	public PlaceCell(NavigationPanel owner)
	{
		this.navigationPanel = owner;
		
		this.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(placeInfo != null)
				{
					PlaceCell.this.navigationPanel.placeScanned(placeInfo);
				}

			}
		});
	}
	
	/**
	 * Sets PlaceInfo to the button
	 * 
	 * @param placeInfo
	 */
	public void setPlaceInfo(PlaceInfo placeInfo) 
	{
		this.placeInfo = placeInfo;
	}

	/**
	 * Gets the place associated to the button
	 * 
	 * @return PlaceInfo
	 */
	public PlaceInfo getPlaceInfo() 
	{
		return placeInfo;
	}


	/**
	 * Notifies that the robot arrives at a place. This PlaceCell will use the information provided in order to update its state
	 * 
	 * @param arrivalPlace - Place where the robot arrives at
	 */
	public void arriveAt(PlaceInfo arrivalPlace)
	{
		
		if(arrivalPlace.isSpaceship())
		{
			this.setBackground(Color.RED);
		}
		else
		{
			this.setBackground(Color.GREEN);
		}
	}
	
	/**
	 * Updates the PlaceCell because that the robot leaves the place
	 * 
	 */
	public void left()
	{
		this.setBackground(Color.GRAY);
	}


}
